<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="com.mobilottery.server.UserManager"%>
<%@page import="java.util.List"%>
<%@page import="com.mobilottery.server.User"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ include file="/pages/template/page-begin.jspf" %>
<%
	_pageTitle = "Users | Admin"; 
	_pageId = "users"; 
	_mainPageId = "admin"; 
%>
<%@ include file="/pages/template/head-begin.jspf" %>
<%@ include file="/pages/template/head-end.jspf" %>

<body>
	<%@ include file="/pages/template/content-begin.jspf" %>
	<%@ include file="/pages/admin/admin-bar-left.jspf" %>
	
	<% 	
		if(request.getParameter("action")!=null)
		{
			String userAction = request.getParameter("action");
			String userName = request.getParameter("userName");
			try
			{
				User user = UserManager.getInstance().getUser(userName);
				if("delete".equals(userAction))
				{
					PersistenceManager.getInstance().delete(user);
				}
				else if("resetPassword".equals(userAction))
				{
					String password = user.resetPassword();
					PersistenceManager.getInstance().persist(user);
					%>
					<center><div style="color: green;margin-top: 15px;position: relative;z-index: 10;">New password for <%=user.getUserName()%>: <%=password%></div></center>
					<%				
				}
				else if("toogleBlock".equals(userAction) && !(user instanceof Admin))
				{
					user.setIsBlocked(!user.isBlocked());
					PersistenceManager.getInstance().persist(user);
				}
			}
			catch(Exception ex)
			{
%>
	<script>alert('error performing action <%=request.getParameter("action")%>: <%=ex.getMessage()%>');</script>
<%				
			}
		}
		
	%>
	
	<div class="fillContent">	
	<div id="content">	
	<h3>Users</h3>
	<div class="contentFillBox contentFillTableBox">
		<table class="dataTable">
			<tr>
				<th class="actionColumn"></th>
				<th class="actionColumn"></th>
				<th class="actionColumn"></th>
				<th class="dataColumn">Username</th>
				<th class="dataColumn" style="width:100px;">E-mail</th>
				<th class="dataColumn">Role</th>
				<th class="dataColumn">First Name</th>
				<th class="dataColumn">Last Name</th>
				<th class="dataColumn">Card</th>
			</tr>
		<%
			List<User> gameUsers = UserManager.getInstance().getAllUsers();
			boolean oddRow = true;
			for(User user:gameUsers)
			{
		%>	
				<tr class="<%=oddRow ? "odd" : "even"%>Row">
					<td class="actionColumn"><a href="?action=delete&userName=<%=user.getUserName()%>" title="Remove User '<%=user.getUserName()%>'">Delete</a></td>
					<td class="actionColumn"><a href="?action=toogleBlock&userName=<%=user.getUserName()%>" title="Block User '<%=user.getUserName()%>' from Accessing the Site"><%=!(user instanceof Admin) ? (user.isBlocked() ? "Unblock" : "Block") : "" %></a><%=(user instanceof Admin)? "x" : ""%></td>
					<td class="actionColumn"><a href="?action=resetPassword&userName=<%=user.getUserName()%>" title="Reset Password">Reset Pass</a></td>
					<td class="dataColumn" title="Username: <%=user.getUserName()%>"><%=user.getUserName()%></td>
					<td class="dataColumn"><a href="mailto:<%=user.getEmail()%>" title="E-mail Address: <%=user.getEmail()%>"><%=user.getEmail()%></a></td>
					<td class="dataColumn" title="User Role: <%=user.getClass().getSimpleName()%>"><%=user.getClass().getSimpleName()%></td>
					<td class="dataColumn" title="First Name: <%=user.getFirstName()%>"><%=user.getFirstName()%></td>
					<td class="dataColumn" title="Last Name: <%=user.getLastName()%>"><%=user.getLastName()%></td>
					<td class="dataColumn" title="First credit card number digits: <%=user.getCreditCardLabel()%>"><%=user.getCreditCardLabel()%></td>
				</tr>
		<%
				oddRow=!oddRow;
			}
		%>
		</table>
	</div>
	</div>
	</div>

	<jsp:include page="/pages/template/content-end.jspf" />
	
</body>
</html>
